using System;
using System.Linq;
using System.Collections.Generic;

namespace Rel.Expressions
{
	public abstract partial class RegBase
	{


		/// <summary>
		/// Represents the base character class represented by this object.
		/// </summary>
		protected  string _patternbase;

		/// <summary>
		/// The regular expression repesented by this.
		/// </summary>
		public virtual string Pattern {
			get {
                

				string pattern = _patternbase;

				switch (quantifier) {
				case QuantifierType.AtLeast:
					pattern += "{" + quantifierMin.ToString () + ",}";
					break;
				case QuantifierType.Between:
					pattern += "{" + quantifierMin.ToString () + "," + quantifierMax.ToString () + "}";
					break;
				case QuantifierType.Exactly:
					pattern += "{" + quantifierMin.ToString () + "}";
					break;
				case QuantifierType.One:
					//pattern += "{1}";
					break;
				case QuantifierType.OneOrMore:
					pattern += "+";
					break;
				case QuantifierType.ZeroOrMore:
					pattern += "*";
					break;
				case QuantifierType.ZeroOrOne:
					pattern += "?";
					break;
				case QuantifierType.None:

					if (this is StartClass || this is Literal ) {
					} else {
						throw new FormatException ("Encountered a quantifier of NONE for Type: "+this.GetType().Name);
					}

					break;
				}

				

				string options = (ignoreCase ? "i" : string.Empty) +
				                 (multiLine ? "m" : string.Empty);

				pattern = (options.Length > 0 ? "(?" + options + ")" : string.Empty) + pattern
				        + (options.Length > 0 ? "(?-" + options + ")" : string.Empty);


                pattern = !UseExtract? pattern :
                    ("(?<" + GroupName + ">(" + pattern + "))");


                switch (anchor)
                {
                    case Anchors.AfterPreviousMatch:
                        pattern = @"\G" + pattern;
                        break;
                    case Anchors.BetweenWordAndNonWord:
                        pattern = @"\b" + pattern + @"\b";
                        break;
                    case Anchors.EndOfLine:
                        pattern = pattern + "$";
                        break;
                    case Anchors.EndOfString:
                        pattern = pattern + @"\z";
                        break;
                    case Anchors.None:
                        break;
                    case Anchors.NotBetweenWordAndNonWord:
                        pattern = @"\B" + pattern;
                        break;
                    case Anchors.StartOfLine:
                        pattern = @"^" + pattern;
                        break;
                    case Anchors.StartOfString:
                        pattern = @"\A" + pattern;
                        break;
                }


				return pattern;
			}
		}

	}
}

